﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using BitPortal.Models;
using BitPortal.Utils;
using Ext.Net;

namespace BitPortal.Admin.Language.Module
{
    public class LanguageResource
    {
        public string Key { set; get; }
        public string Value { set; get; }
    }
    public partial class ResourcesEditor : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void BindData()
        {
            //if (!String.IsNullOrEmpty(Request.QueryString["LanguageCode"]))
            {
                var dic = LanguageBLL.ReadResXFromResourceFile(LanguageBLL.GetResourceFileByLanguageCode(Request.QueryString["LanguageCode"]));
                Store1.DataSource = dic.Select(ent => new LanguageResource() { Key = ent.Key, Value = ent.Value });
                Store1.DataBind();
            }
        }
        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("LanguageEdit");
                var dic = LanguageBLL.ReadResXFromResourceFile(LanguageBLL.GetResourceFileByLanguageCode(Request.QueryString["LanguageCode"]));
                ChangeRecords<LanguageResource> resources = e.DataHandler.ObjectData<LanguageResource>();
                foreach (LanguageResource record in resources.Updated)
                {
                    dic[record.Key] = record.Value;
                }
                LanguageBLL.WriteResXResourceFile(LanguageBLL.GetResourceFileByLanguageCode(Request.QueryString["LanguageCode"]), dic);
                (this.Page as Controls.BaseAdminPage).ShowNotification("Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }
    }
}